<template>
  <el-dialog title="新增人员" :visible="isShow" width="40%" @close="closeFn">
    <el-form :model="perForm" :rules="perRules">
      <el-form-item label="人员名称：" prop="userName">
        <el-input
          v-model="perForm.userName"
          placeholder="请输入"
          maxlength="5"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="角色：" prop="role">
        <el-select v-model="perForm.role" placeholder="请选择" @focus="getRole">
          <el-option
            v-for="item in roleList"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话：" prop="tel">
        <el-input
          v-model="perForm.tel"
          placeholder="请输入"
          maxlength="11"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="负责区域：" prop="region">
        <el-select v-model="perForm.region" placeholder="请选择" @focus="getRegion">
          <el-option
            v-for="item in regionList"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-form-item>

      <!-- 头像上传 -->
      <div class="tx">
        <span>头像：</span>
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="perForm.avatarUrl"
            :src="perForm.avatarUrl"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <span class="tip">支持扩展名：jpg、png，文件不得大于100kb</span>
      <!-- 头像上传 -->
      <!-- 状态 -->
      <div class="status">
        <span>状态：</span>
        <template>
          <!-- `checked` 为 true 或 false -->
          <el-checkbox v-model="perForm.status">是否启用</el-checkbox>
        </template>
      </div>
      <!-- 状态 -->
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button class="quxiao" @click="closeFn">取 消</el-button>
      <el-button class="queren">确 认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRole, getRegion } from '@/api/person'
export default {
  props: {
    isShow: Boolean
  },
  data () {
    return {
      roleList: [],
      regionList: [],
      perForm: {
        userName: '',
        role: '',
        tel: '',
        region: '',
        avatarUrl: '',
        status: ''
      },
      perRules: {
        userName: [{ required: true, message: '请输入人员名称', trigger: 'blur' }],
        role: [{ required: true }],
        tel: [{ required: true, message: '请输入人员名称', trigger: 'blur' }],
        region: [{ required: true }]
      }
    }
  },
  methods: {
    // 图片方法
    handleAvatarSuccess (res, file) {
      this.perForm.avtarUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 关闭
    closeFn () {
      // this.isShow = false
      this.$emit('close', false)
    },
    // 获取角色列表
    async getRole () {
      const { data } = await getRole()
      // console.log(data)
      this.roleList = data
    },
    // 获取区域列表
    async getRegion () {
      const { data: { currentPageRecords } } = await getRegion()
      console.log(currentPageRecords)
      this.regionList = currentPageRecords
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  .el-dialog__body {
    width: 600px;
    height: 100%;
    .el-form {
      .el-form-item {
        width: 500px;
        display: flex;
        justify-content: flex-end;
        .el-form-item__content {
          .el-input {
            width: 396px;
            height: 36px;
          }
        }
      }
    }
  }
}
.tx {
  width: 150px;
  height: 100px;
  margin-left: 50px;
  display: flex;
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    margin-left: 13px;
    border: 1px solid #999;
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
.tip {
  margin-left: 100px;
  color: #999;
}
.status {
  margin-left: 50px;
  margin-top: 30px;
  span {
    margin-right: 13px;
  }
}
.dialog-footer {
  text-align: center;
  .quxiao {
    background-color: #fbf4f0;
  }
  .queren {
    background-color: #ff6a27;
    color: #fff;
  }
}
</style>
